package com.yan;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Author: 汪焰
 * @Date: 2021/7/8 19:55
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://root:root@1.116.195.238:5672/%2f");

        try (final Connection connection = factory.newConnection();
             final Channel channel = connection.createChannel()) {
            Map<String, Object> arguments = new HashMap<>();
            //消息队列中消息过期时间 30s
            arguments.put("x-message-ttl", 60 * 1000);
            //如果消息队列没有消费者，则10s后消息过期
            arguments.put("x-expires", 60 * 1000);

            channel.queueDeclare("queue.ttl.waiting", true, false, false, arguments);
            channel.exchangeDeclare("ex.ttl.waiting", BuiltinExchangeType.DIRECT, true, false, null);
            channel.queueBind("queue.ttl.waiting", "ex.ttl.waiting", "key.ttl.waiting");

            AMQP.BasicProperties build = new AMQP.BasicProperties.Builder()
                    .contentEncoding("utf-8")
                    .deliveryMode(2)
                    .build();

            channel.basicPublish("ex.ttl.waiting", "key.ttl.waiting", build, "等待的订单号".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
